<?php
/**
 * @Author: Wang chunsheng  email:2192138785@qq.com
 * @Date:   2023-05-06 19:34:21
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2023-05-06 19:46:09
 */

namespace App\Admin\Sys;

use Common\AdminController;
use Common\Services\Sys\DeviceService;

class DeviceCateController extends AdminController
{
    protected array $authOptional = ['*'];

    public function add(): array
    {
        $request = $this->request;

        $title = $request->input('title');
        $cate_pid = $request->input('cate_pid');
        $thumb = $request->input('thumb');
        if (empty($title)) {
            return $this->writeJson(400, null, 'title 不能为空');
        }

        $Res = DeviceService::addCate($title, (int) $cate_pid, $thumb);

        return $this->writeJson(200, $Res, '添加成功');
    }

    public function list(): array
    {
        $request = $this->request;
        $page = $request->input('page') ? $request->input('page') : 1;
        $pageSize = $request->input('pageSize') ? $request->input('pageSize') : 10;
        $cate_pid = $request->input('cate_pid');

        $Res = DeviceService::listCate($page, $pageSize, (int) $cate_pid);

        return $this->writeJson(200, $Res, '获取成功');
    }

    public function edit(): array
    {
        $request = $this->request;
        $title = $request->input('title');
        $cate_pid = $request->input('cate_pid');
        $thumb = $request->input('thumb');
        $id = $request->input('id');
        if (empty($id)) {
            return $this->writeJson(400, null, 'id 不能为空');
        }
        if (empty($title)) {
            return $this->writeJson(400, null, 'title 不能为空');
        }

        $Res = DeviceService::editCate($id, $title, (int) $cate_pid, $thumb);

        return $this->writeJson(200, $Res, '编辑成功');
    }

    public function delete(): array
    {
        $request = $this->request;
        $id = $request->input('id');
        if (empty($id)) {
            return $this->writeJson(400, null, 'id 不能为空');
        }

        $Res = DeviceService::deleteCate($id);

        return $this->writeJson(200, $Res, '删除成功');
    }
}
